package com.chen.gulimall.gulimallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.chen.gulimallcommon.common.utils.PageUtils;
import com.chen.gulimallcommon.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import com.chen.gulimall.gulimallproduct.entity.BrandEntity;
import com.chen.gulimall.gulimallproduct.service.BrandService;


/**
 * 品牌
 *
 * @author mr.chen
 * @email 3061634906@qq.com
 * @date 2022-04-18 22:06:56
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("gulimallproduct:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("gulimallproduct:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     *  根据id获取品牌信息
     * @param brandIds
     * @return
     */
    @GetMapping("/infos")
    public R info(@RequestParam("brandIds")List<Long> brandIds) {
        List<BrandEntity> brand = brandService.getBrandsByIds(brandIds);

        return R.ok().put("brand", brand);
    }



    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("gulimallproduct:brand:save")
    //必须加此 @Valid 注解才能将效验数据的注解生效(@URL)
    public R save(@RequestBody BrandEntity brand ,BindingResult binding){
          brandService.save(brand);
//        Map<String,String> map = null;
//        binding.getFieldErrors().forEach((item) -> {
//            String message = item.getDefaultMessage();
//            String field = item.getField();
//            map.put(message,field);
//        });
//
//		brandService.save(brand);

        return R.ok();
        //return R.error(400,"提交的数据不合法").put("data",map);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("gulimallproduct:brand:update")
    public R update(@RequestBody BrandEntity brand){
		brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("gulimallproduct:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
